package org.example.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.lingyangwl.framework.core.response.Resp;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Slf4j
@RestController
public class UserController {
    @Resource
    private WxMaService wxMaService;

    /**
     * 获取openId
     *
     * @param code: 微信登录code
     */
    @GetMapping("/getOpenId")
    public Resp<String> getOpenId(String code) throws WxErrorException {
        String openid = wxMaService.getUserService().getSessionInfo(code).getOpenid();
        log.info("code: {}, openId: {}", code, openid);
        return Resp.ok(openid);
    }
}
